package com.exahq.controller;

import com.exahq.common.lang.Result;
import com.exahq.entity.*;
import com.exahq.service.PartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhq
 * @since 2021-02-06
 */
@RestController
@RequestMapping("/partner")
public class PartnerController {
    @Autowired
    PartnerService partnerService;

    /**
     * 带条件分页
     * @param query
     * @param response
     * @return
     */
    @PostMapping("/partners")
    public Result partners(@RequestBody Map<String,String> query, HttpServletResponse response) {
        SelectData selectData =new SelectData();
        Integer currentPage = Integer.valueOf(query.get("pageIndex"));
        selectData.setPageIndex(currentPage);
        System.out.println(query.get("sex"));
        if(currentPage == null || currentPage < 1) selectData.setPageIndex(0);
        selectData.setNation(query.get("nation"));
        selectData.setAddress(query.get("address"));
        selectData.setSex(query.get("sex").trim());
        selectData.setEducation(query.get("education"));
        selectData.setName(query.get("name"));
        selectData.setPageSize(Integer.valueOf(query.get("pageSize")));
        List<Partner> partnerList =partnerService.getByQuery(selectData);
        int totalCount = partnerService.getCount(selectData);
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put("partnerList",partnerList);
        map.put("totalCount",totalCount);
        return Result.succ(map);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable(name = "id") Integer id) {
        Partner partner = partnerService.getById(id);
        return Result.succ(partner);
    }

    @GetMapping("/finaAll")
    public Result finaAll() {
        List<Partner> partnerList =partnerService.list();
        return Result.succ(partnerList);
    }

    /**
     * 修改/新增
     * @param partner
     * @return
     */
    @PostMapping("saveEdit")
    public Result saveEdit(@RequestBody Partner partner) {
        if (partner!=null && !partner.equals("")){
            System.out.println(partner);
            if (partner.getPid() == null){
                boolean b = partnerService.save(partner);
                if (b){
                    List<Partner> partnerList =partnerService.list();
                    return Result.succ("新添成功",partnerList);
                }
            }else {
                if (partnerService.saveOrUpdate(partner))
                    return Result.succ("修改成功",partnerService.saveOrUpdate(partner));
            }
        }
            return Result.succ(null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public Result del(@PathVariable(name = "id") Integer id) {
        return Result.succ( partnerService.delete(id));
    }



}
